<?php
namespace app\admin\controller;
use app\service\communityService;
use app\service\deviceService;
use app\service\roominfoService;
use Swoole\Client;
use think\facade\Db;
use think\facade\Log;
use think\facade\View;

class Qrcode extends Common{
	public function list(){

		$com = $this->pidTokenId();
		$whe=array();



        $pagelist =Db::name('qrcode')->where($whe)->order('id asc')->paginate(20);;
        $number = Db::name('qrcode')->where($whe)->count();;
        if(!empty($number)){
            foreach($pagelist as $lis){

                $list[] = $lis;
            }
            View::assign('list', $list);
        }else{
            View::assign('list', $pagelist);
        }
        View::assign('number',$number);
		//View::assign("list",$pagelist);
        View::assign("pagelist",$pagelist);
		return View::fetch("list");
	}

	public function add(){

        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $number=$data["number"];
            $list=[];
            for ($i=0;$i<$number;$i++){
                $adddata=array(
                    "create_time"=>date('Y-m-d H:i:s')
                );
                $list[] = $adddata;
            }
            //var_dump($list);

            $res=Db::name('qrcode')->insertAll($list);
            if($res){
                return "批量添加成功";
            }else{
                $this->error('添加失败',url('add'));
            }
        }
        return View::fetch('add');
		
	}
    public function edit(){
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('设备信心不存在');
            }
            $info = (new deviceService())->getInfo($id);
            View::assign('info',$info);
            return View::fetch('edit');
        }else{
            $data = request()->post();
            $validate=  $this->validate($data,[
                'devicekey|设备序列号' => 'require',

            ]);
            if($validate !== true){
                $this->error($validate);
            }
            $result = (new deviceService())->edit($data);
            if($result){
                return "修改成功";
                $this->success('修改成功',url('list'));
            }else{
                $this->error('修改失败');
            }
        }
    }

}